Program Example

 # program to check if a number is prime number
def isPrime(n):
if (n <= 1):
return False

# Check from 2 to n-1
for i in range(2, n):
if (n % i == 0):
return False
return True

if isPrime(10):
print("true")
else:
print("false")
# Python program to check string is palindrome  or not
x = "malayalam"
w = ""
for i in x:
w = i + w
if (x == w):
print("Yes")
else:
print("No")
# Python program to reverse a string
w=""
value = input("Please enter a string:\n")
print("You entered",type(value))
for i in value :
w = i + w
print(w)
def reversestr(string):
strlist = []
i = len(string) - 1
while (i >= 0):
strlist.append(string[i])
i -= 1
return ''.join(strlist)
print(reversestr('Vinayak'))
# Function to reverse A[] from start to end
def reverseList(A, start, end):
while start < end:
A[start], A[end] = A[end], A[start]
start += 1
end -= 1

# Driver function to test above function
A = [1, 2, 3, 4, 5, 6,7,8]
print(A)
n=(len(A)) - 1
reverseList(A, 0, n)
print("Reversed list is")
print(A)
# A Python program to find the first, 
# second and third minimum element
# in an array

MAX = 100000

def Print3Smallest(arr, n):
firstmin = MAX
secmin = MAX
thirdmin = MAX

for i in range(0, n):

if arr[i] < firstmin:
thirdmin = secmin
secmin = firstmin
firstmin = arr[i]

elif arr[i] < secmin:
thirdmin = secmin
secmin = arr[i]

elif arr[i] < thirdmin:
thirdmin = arr[i]

print("First min = ", firstmin)
print("Second min = ", secmin)
print("Third min = ", thirdmin)

# driver program
arr = [4, 9, 1, 32, 3]
n = len(arr)
Print3Smallest(arr, n)
# Python3 program to count the
# number of occurrences of a
# particular digit in a number

# Function to count the occurrences
# of the digit D in N
def countOccurrances(n, d):
count = 0

# Loop to find the digits of N
while (n > 0):

# check if the digit is D
if(n % 10 == d):
count = count + 1

n = n // 10

# return the count of the
# occurrences of D in N
return count

# Driver code
d = 1
n = 214215421
print(countOccurrances(n, d))

Linear Search

def search(arr,n,x):
for i in range (0, n):
if(arr[i] == x):
return i
return -1

arr= [2,3,5,10,8,3]
x=10
n=len(arr)

result = search(arr,n,x)

if (result== -1):
print("Elemnt is not persent")
else:
print("element persent into list",result)

No comments:

Post a Comment